#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::time::Duration;
use serde_json::{Result, Value};
use tauri::{Manager,Window};
use serde::{Serialize, Deserialize};
mod TcpServer;
pub use TcpServer::*;
use async_std::task;
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}
#[derive(Debug,Serialize,Clone,Deserialize)]
struct Payload{
    message:String
}
#[tauri::command]
fn greet1(window: Window,name:String) {
    println!("{}",window.label());
    println!("{}",name);
    window.listen_global("click",|event| {
            println!("{:?}",event.payload().unwrap());
            let v: Payload = serde_json::from_str(event.payload().unwrap()).unwrap();
            println!("{:?}",v.message);
    });
    task::spawn(async move{
        loop{
             window.emit_all("test", Payload{
                    message:"666".to_string()
            });
            std::thread::sleep(Duration::from_secs(1));
        }
    });
}
fn main(){
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet,greet1,newServer,getIP])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
